package com.notepad.db.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.notepad.db.DBManager;
import com.notepad.db.entity.DBNoteEntity;
import com.notepad.util.Constant;

public class DatabaseDaoImp implements DatabaseDao{

	private SQLiteDatabase database;
	
	public DatabaseDaoImp(Context context) {
		database = DBManager.getInstance(context).getWriteableDatabase();
	}
	
	@Override
	public void insert(DBNoteEntity dbnote) {
		
		ContentValues cv = new ContentValues();
		cv.put(Constant.COLUMNS_NOTE.ID, dbnote.id);
		cv.put(Constant.COLUMNS_NOTE.TITLE, dbnote.title);
		cv.put(Constant.COLUMNS_NOTE.TIME,dbnote.time );
		cv.put(Constant.COLUMNS_NOTE.CONTENT, dbnote.content);
		database.insert(Constant.TABLE.NOTE, null, cv);
	}

	@Override
	public void delete(String id) {
		database.delete(Constant.TABLE.NOTE, Constant.COLUMNS_NOTE.ID + " = ?", new String[]{id});
	}

	@Override
	public void update(DBNoteEntity dbnote) {
		ContentValues cv = new ContentValues();
		cv.put(Constant.COLUMNS_NOTE.TITLE, dbnote.title);
		cv.put(Constant.COLUMNS_NOTE.TIME,dbnote.time );
		cv.put(Constant.COLUMNS_NOTE.CONTENT, dbnote.content);
		database.update(Constant.TABLE.NOTE, cv, Constant.COLUMNS_NOTE.ID + "= ?", new String[]{dbnote.id});
	}

	
	@Override
	public ArrayList<DBNoteEntity> getALLNotes() {
		ArrayList<DBNoteEntity> list = new ArrayList<DBNoteEntity>();
		String sql = "select * from "+Constant.TABLE.NOTE+" ORDER BY id DESC";
		Cursor c = database.rawQuery(sql, null);
		if(c == null){
			return null;
		}
		
		while(c.moveToNext()){
			DBNoteEntity dbnote = new DBNoteEntity();
			dbnote.id = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.ID));
			dbnote.title = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.TITLE));
			dbnote.time = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.TIME));
			dbnote.content = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.CONTENT));
			list.add(dbnote);	
		}
		return list;
	}

	@Override
	public DBNoteEntity findNoteById(String id) {
		String sql = "select * from " + Constant.TABLE.NOTE + " where " +Constant.COLUMNS_NOTE.ID +" = ?";
		Cursor c = database.rawQuery(sql, new String[]{id});
		DBNoteEntity dbnote = new DBNoteEntity();
		dbnote.id = id;
		dbnote.title = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.TITLE));
		dbnote.time = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.TIME));
		dbnote.content = c.getString(c.getColumnIndex(Constant.COLUMNS_NOTE.CONTENT));
		
		return dbnote;
	}
	
	public void add(List<DBNoteEntity> notes){
		database.beginTransaction();
		for(DBNoteEntity note:notes){
			database.execSQL("insert into"+Constant.TABLE.NOTE+"VALUES(null,?,?,?)", new Object[]{note.title,note.time,note.content});
		}
		database.setTransactionSuccessful();
		database.endTransaction();
		
	}

	@Override
	public void insertOrUpdate(DBNoteEntity dbnote) {
		if(dbnote.id != null){
			update(dbnote);
		}else{
			insert(dbnote);
		}
	}

}
